package com.example.baseframework.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;
import android.widget.Scroller;


public class DownDrawerView extends FrameLayout {
    private int mLastY;
    private int mLastInterceptX;
    private int mLastInterceptY;
    private final Scroller mScroller;
    private final VelocityTracker mVelocityTracker;
    private final int mTouchSlop;

    public DownDrawerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mScroller = new Scroller(context);
        mVelocityTracker = VelocityTracker.obtain();
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public DownDrawerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DownDrawerView(Context context) {
        this(context, null);
    }


    public void setNoScroll(boolean isScroll) {
        this.isScroll = isScroll;
    }

    private boolean isScroll = true;

    private boolean currGlideState = false;

    public void closeDrawer() {
        currGlideState = false;
        checkDrawer();
    }

    public void openDrawer() {
        currGlideState = true;
        checkDrawer();
    }

    private void checkDrawer() {
        int dy = currGlideState ? getHeight() : -getHeight();
        smoothScrollBy(dy);
    }


//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if (isScroll || !currGlideState) {
//            return false;
//        }
//        boolean isIntercept = false;
//        int x = (int) ev.getX();
//        int y = (int) ev.getY();
//
//        switch (ev.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                if (!mScroller.isFinished()) {
//                    mScroller.abortAnimation();// ????
//                    isIntercept = true;
//                }
//                break;
//            case MotionEvent.ACTION_MOVE:
//                int deltaX = x - mLastInterceptX;
//                int deltaY = y - mLastInterceptY;
//                if (Math.abs(deltaX) < Math.abs(deltaY) && Math.abs(deltaY) >= mTouchSlop && deltaY > 0) {
//                    //横向滑动的距离大于竖直滑动距离并且大于最小能识别成滑动的距离 并且方向向下
//                    isIntercept = true;
//                } else {
//                    isIntercept = false;
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//                isIntercept = false;
//                break;
//        }
//
//        mLastInterceptX = x;
//        mLastInterceptY = y;
//        mLastY = y;
//        return isIntercept;
//    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (isScroll || !currGlideState) {
            return false;
        }
        int y = (int) event.getY();
        mVelocityTracker.addMovement(event);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaY = y - mLastY;
                final int height = getHeight();
                if (Math.abs(deltaY) < mTouchSlop || (deltaY < 0 && getScrollY() >= height)) {
                    return false;
                }
                if (getScrollY() - deltaY > height) {
                    deltaY = getScrollY() - height;
                }
                scrollBy(0, -deltaY);
                pageScrolled();
                break;
            case MotionEvent.ACTION_UP:
                int scrollY = getScrollY();
                mVelocityTracker.computeCurrentVelocity(1000);
                float velocityY = mVelocityTracker.getYVelocity();
                boolean isGlide = false;
                if (Math.abs(velocityY) > 300) {
                    if (velocityY > 0) {
                        isGlide = true;
                    }
                } else {
                    if (scrollY < getHeight() / 2) {
                        isGlide = true;
                    }
                }
                int dy = isGlide ? -scrollY : getHeight() - scrollY;
                currGlideState = !isGlide;
                smoothScrollBy(dy);
                mVelocityTracker.clear();
                break;
        }
        mLastY = y;
        return true;
    }

    private OnPageChangeListener mPageChangeListener;

    public void setPageChangeListener(OnPageChangeListener listener) {
        mPageChangeListener = listener;
    }

    private void pageScrolled() {
        int scrollY = getScrollY();
        if (mPageChangeListener != null) {
            mPageChangeListener.onPageChange((float) scrollY / getHeight());

        }
    }


    private void smoothScrollBy(int dy) {
        mScroller.startScroll(0, getScrollY(), 0, dy, 550);
        invalidate();
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            pageScrolled();
            postInvalidate();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        mVelocityTracker.recycle();
        super.onDetachedFromWindow();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        // final int childCount = getChildCount();
        // for (int i = 0; i < childCount; i++) {
        //     final View childView = getChildAt(i);
        //     childView.layout(0, getHeight(), getWidth(), getHeight() + childView.getHeight());
        // }
    }

    public interface OnPageChangeListener {
        void onPageChange(float change);
    }

}
